package com.gauravk.audiovisualizersample.ui;

import com.gauravk.audiovisualizer.visualizer.HiFiVisualizer;
import com.gauravk.audiovisualizersample.ResourceTable;
import com.gauravk.audiovisualizersample.util.MusicFinish;
import com.gauravk.audiovisualizersample.util.PlaySoundUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import ohos.media.audio.AudioRenderer;

public class HiFiAbility extends Ability {
    private HiFiVisualizer mVisualizer;
    private PlaySoundUtil playSoundUtil;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_activity_hi_fi);

        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.rgb(66, 119, 208));

        playSoundUtil = new PlaySoundUtil();
        mVisualizer = (HiFiVisualizer) findComponentById(ResourceTable.Id_hifi);
        mVisualizer.setAudioSessionId(playSoundUtil.getAudioRenderer().getRendererSessionId());
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (playSoundUtil.getAudioRenderer().getState() != AudioRenderer.State.STATE_PLAYING) {
                    playSoundUtil.loadSound("Ring10.wav", new MusicFinish() {
                        @Override
                        public void finish() {
                            mVisualizer.setVisualizationEnabled(false);
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        playSoundUtil.getAudioRenderer().stop();
    }
}
